﻿Imports System.IO


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim Client As New UNOLibs.Net.ClientClass
    WithEvents Server As UNOLibs.Net.ServerClass

    ''the above the lines is for server function

    Dim objPLSXFile As New IniFile(PlaylistFileName)
    Public RadioURL As String
    Dim ShowTips As Boolean = True
    ''  Dim StartInTray As Boolean = False
    Dim AutoPlay As Boolean = False
    Dim Language As String = "en"
    Public RadioName As String = ""
    Dim PlaylistFileName As String = ""
    Dim LastPathUsed As String = Application.StartupPath
    Dim KeepHistory As Boolean = False
    Dim ExtractTitle As Boolean = False
    ''Dim Slot As Integer = 1
    Dim ReturnedTitle As String
    Dim FromCommand As Boolean = False
    Dim CurrentTitle As String
    Dim FavoritesPath As String = Application.StartupPath & "\favorites.txt"
    Dim TimerRounds As Integer = 0
    Dim ShowTitle As Boolean
    Dim AutoClose As Boolean
    Dim NoHotKeys As Boolean = False
    Dim WaitForRemoteCommands As Boolean = False
    Public SenderToReply As String = "127.0.0.1"




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        Me.Visible = False

        Try
            NoHotKeys = objIniFile.GetString("settings", "nohotkeys", False)
        Catch ex As Exception
            'code here to notify user that something went wrong with ini settings (to be added)
            NoHotKeys = False
        End Try
        If NoHotKeys = False Then
            Hotkey.RegisterHotKey(Me, 1, "F", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 2, "S", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 3, "P", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 4, "T", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 5, "X", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 6, "H", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 7, "O", Hotkey.KeyModifier.Alt)
            Hotkey.RegisterHotKey(Me, 8, "M", Hotkey.KeyModifier.Alt)
        End If

        Call CheckINI()
        LoadFavorites()
      
        Action = "Start"
        'Button1.Text = Action
        'Label1.Text = ""
        NotifyIcon1.Text = Action
        If WaitForRemoteCommands = True Then
            NotifyIcon1.Icon = My.Resources.XplodeStartWEB
        Else
            NotifyIcon1.Icon = My.Resources.PlayWoofer2
        End If


        NotifyIcon1.Visible = True
        Call CheckINI()
        LoadFavorites()

        If WaitForRemoteCommands = True Then
            Try
                Server = New UNOLibs.Net.ServerClass(39999, True, "C:\")
                Server.IncomingPath = "C:\Incoming Files"
                'Label2.Text = "Server is running"
            Catch ex As Exception

            End Try
        End If
        If ShowTips = True Then
            If GetSetting("XtreMP3", "Settings", "Version", "") <> Application.ProductVersion Then
                NotifyIcon1.BalloonTipText = "Welcome to XtreMP3 player. ver. " & Application.ProductVersion & _
                    vbNewLine & "This tiny player created in order be used directly from system tray. " & vbNewLine _
                      & "You can manage it by using the tray functions and hot keys provided. " & vbNewLine _
                        & ". Click to Play/Stop." & vbNewLine _
                        & ". Right click to set options."
                NotifyIcon1.BalloonTipTitle = "XtreMP3 - First run message"
                NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
                ' NotifyIcon1.
                NotifyIcon1.ShowBalloonTip(2500)

                SaveSetting("XtreMP3", "Settings", "Version", Application.ProductVersion)
            End If
        End If

    End Sub

    'Code for hotkeys check below
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = Hotkey.WM_HOTKEY Then
            Hotkey.handleHotKeyEvent(m.WParam)
        End If
        MyBase.WndProc(m)
    End Sub 'System wide hotkey event handling
    'End of hotkeys code

    Sub CheckINI()
        If System.IO.File.Exists(Application.StartupPath & "\settings.ini") = True Then
            Call LoadValues()
        Else
            ' create ini file
            '  objIniFile.WriteString("settings", "top", -30450)
            '  objIniFile.WriteString("settings", "left", -30450)
            objIniFile.WriteString("settings", "volume", 50)
            objIniFile.WriteString("settings", "radiourl", "http://www.mediabox.gr/streams/rockfm.asx")
            objIniFile.WriteString("settings", "radioname", "Rock 969 Athens")
            objIniFile.WriteString("settings", "autoplay", False)
            ' objIniFile.WriteString("settings", "startintray", "false")
            ' objIniFile.WriteString("settings", "language", "gr")
            objIniFile.WriteString("settings", "showtips", True)
            ' objIniFile.WriteString("settings", "keephistory", False)
            objIniFile.WriteString("settings", "extracttitle", False)
            objIniFile.WriteString("settings", "showtitle", True)
            objIniFile.WriteString("settings", "waitforremotecommands", False)
            objIniFile.WriteString("settings", "autoclose", True)
            objIniFile.WriteString("settings", "nohotkeys", False)
            objIniFile.WriteString("settings", "mutetime", 10)
            objIniFile.WriteString("settings", "mutelevel", 2)
            objIniFile.WriteString("OpenLinkWindow", "RadioURL", "http://www.mediabox.gr/streams/rockfm.asx")
            objIniFile.WriteString("OpenLinkWindow", "RadioName", "Rock 969 Athens")
            objIniFav.WriteString("1", "RadioURL", "http://80.237.210.81:80")
            objIniFav.WriteString("1", "RadioName", "Rocky FM")
            objIniFav.WriteString("2", "RadioURL", "http://www.e-radio.gr/asx/noiselamia.asx")
            objIniFav.WriteString("2", "RadioName", "Noise Web Radio")
            objIniFav.WriteString("3", "RadioURL", "http://66.220.3.52:9020")
            objIniFav.WriteString("3", "RadioName", "1.FM smooth Jazz")
            objIniFav.WriteString("4", "RadioURL", "http://94.23.7.16:8000")
            objIniFav.WriteString("4", "RadioName", "Planet Rock")
            objIniFav.WriteString("5", "RadioURL", "http://65.99.205.233:7018")
            objIniFav.WriteString("5", "RadioName", "At Work")
            objIniFav.WriteString("6", "RadioURL", "http://scfire-ntc-aa02.stream.aol.com:80/stream/1048")
            objIniFav.WriteString("6", "RadioName", "Radio Paradise")
            objIniFav.WriteString("7", "RadioURL", "http://tvradio.ert.gr/radio/liveradio/asx/kosmos.asx")
            objIniFav.WriteString("7", "RadioName", "Kosmos Radio")
            objIniFav.WriteString("8", "RadioURL", "http://91.121.91.110:3000")
            objIniFav.WriteString("8", "RadioName", "M2 Analog (80's Only)")
            objIniFav.WriteString("9", "RadioURL", "http://208.85.243.168:8010")
            objIniFav.WriteString("9", "RadioName", "Soft 'N Easy Net Radio")
            objIniFav.WriteString("10", "RadioURL", "http://194.50.90.18:10006")
            objIniFav.WriteString("10", "RadioName", "Smooth Radio London")
            '  objIniFile.WriteString("settings", "slot", 1)
            Beep()
            Call LoadValues()
            Beep()
        End If
    End Sub
    Sub LoadValues()
        ' Dim TempInt As Integer = 100
        '  Dim TempBool As Boolean = True


        ' On Error Resume Next
        'StartInTray = objIniFile.GetString("settings", "startintray", False)
        'If StartInTray = True Then
        '    Me.Hide()
        Me.Opacity = 0
        Me.ShowInTaskbar = False
        'Else
        'End If
        Try
            Me.Top = GetSetting("XtreMP3", "Settings", "Top", -30450)   ' objIniFile.GetString("settings", "top", "-30450")
            Me.Left = GetSetting("XtreMP3", "Settings", "Left", -30450) ' objIniFile.GetString("settings", "left", "-30450")
            LastPathUsed = GetSetting("XtreMP3", "Settings", "LastPathUsed", "empty") ' objIniFile.GetString("settings", "lastpathused", "empty")
            RadioURL = objIniFile.GetString("settings", "radiourl", "http://www.mediabox.gr/streams/rockfm.asx")
        Catch ex As Exception

        End Try
   

        Try
            VolLevel = objIniFile.GetString("settings", "volume", 50)
        Catch ex As Exception
            VolLevel = 50
        End Try
        Player.settings.volume = VolLevel

        Try
            ShowTips = objIniFile.GetString("settings", "showtips", True)
        Catch ex As Exception
            ShowTips = True
        End Try

        Try
            AutoPlay = objIniFile.GetString("settings", "AutoPlay", False)
        Catch ex As Exception
            AutoPlay = False
        End Try

        Try
            ExtractTitle = objIniFile.GetString("settings", "ExtractTitle", False)
        Catch ex As Exception
            ExtractTitle = False
        End Try

        Try
            KeepHistory = objIniFile.GetString("settings", "KeepHistory", False)
        Catch ex As Exception
            KeepHistory = False
        End Try

        Try
            ShowTitle = objIniFile.GetString("settings", "showtitle", True)
        Catch ex As Exception
            ShowTitle = True
        End Try

        Try
            AutoClose = objIniFile.GetString("settings", "autoclose", True)
        Catch ex As Exception
            AutoClose = True
        End Try


        Try
            WaitForRemoteCommands = objIniFile.GetString("settings", "waitforremotecommands", False)
        Catch ex As Exception
            WaitForRemoteCommands = False
        End Try


        Try
            MuteTime = objIniFile.GetString("settings", "mutetime", 10)
        Catch ex As Exception
            MuteTime = 10
        End Try

        Try
            MuteLevel = objIniFile.GetString("settings", "mutelevel", 2)
        Catch ex As Exception
            MuteLevel = 2
        End Try



        RadioName = objIniFile.GetString("settings", "radioname", "Radio station name not available")

        If LastPathUsed = "empty" Then
            LastPathUsed = Application.StartupPath
        Else

        End If
        ' LoadRecent()

        If AutoPlay = True Then
            If ExtractTitle = True Then
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 2)
            End If
            Player.URL = RadioURL
            Player.Ctlcontrols.play()
            If WaitForRemoteCommands = True Then
                NotifyIcon1.Icon = My.Resources.XplodeStopWEB
            Else
                NotifyIcon1.Icon = My.Resources.StopWoofer2
            End If
            Action = "Stop"
            Timer1.Enabled = True
            Dim temptext As String = "Stop " & RadioName & vbNewLine & RadioURL.ToString
            If temptext.Length < 64 Then
                NotifyIcon1.Text = temptext
            Else
                NotifyIcon1.Text = "Stop " & RadioName
            End If
            PlayToolStripMenuItem.Enabled = False
            StopToolStripMenuItem.Enabled = True
        Else
            If ExtractTitle = True Then
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 1)
            End If
            Dim temptext As String = "Play " & RadioName & vbNewLine & RadioURL.ToString
            If temptext.Length < 64 Then
                NotifyIcon1.Text = temptext
            Else
                NotifyIcon1.Text = "Play " & RadioName
            End If
            PlayToolStripMenuItem.Enabled = True
            StopToolStripMenuItem.Enabled = False

        End If
        ToolStripMenuItem2.Text = RadioName
        If ExtractTitle = True Then
            SaveSetting("XtreMP3", "LCDSmartieIntergration", "Station", RadioName)
        End If

    End Sub
    Sub LoadFavorites()
        On Error Resume Next
        Dim s1 = objIniFav.GetString(1, "RadioName", "")
        Dim s2 = objIniFav.GetString(2, "RadioName", "")
        Dim s3 = objIniFav.GetString(3, "RadioName", "")
        Dim s4 = objIniFav.GetString(4, "RadioName", "")
        Dim s5 = objIniFav.GetString(5, "RadioName", "")
        Dim s6 = objIniFav.GetString(6, "RadioName", " ")
        Dim s7 = objIniFav.GetString(7, "RadioName", " ")
        Dim s8 = objIniFav.GetString(8, "RadioName", " ")
        Dim s9 = objIniFav.GetString(9, "RadioName", " ")
        Dim s10 = objIniFav.GetString(10, "RadioName", " ")
        If s1.Length > 1 Then
            If s1 <> s2 And s1 <> s3 And s1 <> s4 And s1 <> s5 Then
                FaToolStripMenuItem.Text = s1
                FaToolStripMenuItem.Visible = True
            Else
                FaToolStripMenuItem.Visible = False
            End If

        End If
        If s2.Length > 1 Then
            If s2 <> s3 And s2 <> s4 And s2 <> s5 Then
                FbToolStripMenuItem.Text = s2
                FbToolStripMenuItem.Visible = True
            Else
                FbToolStripMenuItem.Visible = False
            End If

        End If
        If s3.Length > 1 Then
            If s3 <> s4 And s3 <> s5 Then
                FcToolStripMenuItem.Text = s3
                FcToolStripMenuItem.Visible = True
            Else
                FcToolStripMenuItem.Visible = False
            End If

        End If
        If s4.Length > 1 Then
            If s4 <> s5 Then
                FdToolStripMenuItem.Text = s4
                FdToolStripMenuItem.Visible = True
            Else
                FdToolStripMenuItem.Visible = False
            End If

        End If
        If s5.Length > 1 Then
            FeToolStripMenuItem.Text = s5
            FeToolStripMenuItem.Visible = True
        Else
            FeToolStripMenuItem.Visible = False
        End If

        If s6.Length > 2 Then
            FgToolStripMenuItem.Text = s6
            FgToolStripMenuItem.Visible = True
        Else
            FgToolStripMenuItem.Visible = False
        End If

        If s7.Length > 2 Then
            FhToolStripMenuItem.Text = s7
            FhToolStripMenuItem.Visible = True
        Else
            FhToolStripMenuItem.Visible = False
        End If

        If s8.Length > 2 Then
            FiToolStripMenuItem.Text = s8
            FiToolStripMenuItem.Visible = True
        Else
            FiToolStripMenuItem.Visible = False
        End If
        If s9.Length > 2 Then
            FjToolStripMenuItem.Text = s9
            FjToolStripMenuItem.Visible = True
        Else
            FjToolStripMenuItem.Visible = False
        End If

        If s10.Length > 2 Then
            FkToolStripMenuItem.Text = s10
            FkToolStripMenuItem.Visible = True
        Else
            FkToolStripMenuItem.Visible = False
        End If

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not Server Is Nothing Then
            If Server.IsRunning Then
                Server.StopServer()
            End If
        End If

        If NoHotKeys = False Then
            Hotkey.unregisterHotkeys(Me)
        End If

        Call SaveValues()
    End Sub

    Sub SaveValues()
        On Error GoTo Errh

        If ExtractTitle = True Then
            SaveTextToFile("XtreMP3", Application.StartupPath & "\title.txt")
            SaveSetting("XtreMP3", "LCDSmartieIntergration", "Title", "XtreMP3")
            SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 0)
            SaveSetting("XtreMP3", "LCDSmartieIntergration", "RawStatus", " ")
        End If

        SaveSetting("XtreMP3", "Settings", "Top", Me.Top) ' objIniFile.WriteString("settings", "top", Me.Top)
        SaveSetting("XtreMP3", "Settings", "Left", Me.Left) ' objIniFile.WriteString("settings", "left", Me.Left)
        objIniFile.WriteString("settings", "volume", Player.settings.volume)
        'objIniFile.WriteString("settings", "lastpathused", LastPathUsed)
        SaveSetting("XtreMP3", "Settings", "LastPathUsed", LastPathUsed)
        'objIniFile.WriteString("settings", "slot", Slot)
        objIniFile.WriteString("settings", "radiourl", RadioURL)
        objIniFile.WriteString("settings", "radioname", RadioName)
        'If Me.Text = "" Then
        '    objIniFile.WriteString("settings", "stripplayer", True)
        'Else
        '    objIniFile.WriteString("settings", "stripplayer", False)
        'End If
        Exit Sub
Errh:
        NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
        NotifyIcon1.BalloonTipText = "Error number: " & Err.Number & ". Error description: " & Err.Description & ". Trying to resume..."
        NotifyIcon1.BalloonTipTitle = "Error..."
        NotifyIcon1.ShowBalloonTip(1000)
        Resume Next

    End Sub
    Sub InformServer()

        Try
            If WaitForRemoteCommands = True Then
                Client.SendMessage(SenderToReply, 40000, NotifyIcon1.Text)
            End If
        Catch
        End Try


    End Sub


    Public Sub PlayStop()
        Dim FromPause As Boolean = False
        Dim BackupLink As String = objIniFile.GetString("settings", "radiourl", "http://www.mediabox.gr/streams/rockfm.asx")
        Dim BackupTitle As String = objIniFile.GetString("settings", "radioname", "Rock Athens")
        If Action = "Start" Or Action = "Play" Then
            Try
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "RawStatus", "")
                Player.URL = RadioURL
                Player.Ctlcontrols.play()
                If WaitForRemoteCommands = True Then
                    NotifyIcon1.Icon = My.Resources.XplodeStopWEB

                Else
                    NotifyIcon1.Icon = My.Resources.StopWoofer2
                End If


                Action = "Stop"
                Timer1.Enabled = True
                PlayToolStripMenuItem.Enabled = False
                StopToolStripMenuItem.Enabled = True
                ToolStripMenuItem2.Text = RadioName
                TimerClose.Enabled = False

                If ExtractTitle = True Then
                    SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 2)
                    SaveSetting("XtreMP3", "LCDSmartieIntergration", "Station", RadioName)
                End If

                CurrentTitle = Microsoft.VisualBasic.Left(Player.currentMedia.getItemInfo("Title"), 63)
                If CurrentTitle <> ReturnedTitle Then
                    ReturnedTitle = CurrentTitle
                    NotifyIcon1.Text = CurrentTitle
                    If ExtractTitle = True Then
                        SaveTextToFile(CurrentTitle, Application.StartupPath & "\title.txt")
                        SaveSetting("XtreMP3", "LCDSmartieIntergration", "Title", CurrentTitle)
                    End If
                    '''''''Call server notification sub below()
                    Call InformServer()


                End If
                If ShowTitle = True Then
                    PopUpTittle()
                End If

            Catch ex As Exception
                ' Clipboard.SetText(Err.Number.ToString & Err.Description)
                If Math.Abs(Err.Number) = 1072884951 Then
                    NotifyIcon1.BalloonTipText = "Please check the station link and try again"
                    NotifyIcon1.BalloonTipTitle = "Empty or invalid memory"
                    NotifyIcon1.ShowBalloonTip(1000)
                    RadioURL = BackupLink
                    RadioName = BackupTitle

                    Exit Sub
                End If
                NotifyIcon1.BalloonTipText = "An error occurred while connecting - playing." & vbNewLine & "Error Code: " & Err.Number
                NotifyIcon1.BalloonTipTitle = "Error playing..."
                NotifyIcon1.ShowBalloonTip(1000)

            End Try

        ElseIf Action = "Stop" Then
            Player.Ctlcontrols.stop()
            Action = "Start"
            If WaitForRemoteCommands = True Then
                NotifyIcon1.Icon = My.Resources.XplodeStartWEB
            Else
                NotifyIcon1.Icon = My.Resources.PlayWoofer2
            End If


            PlayToolStripMenuItem.Enabled = True
            StopToolStripMenuItem.Enabled = False
            If ExtractTitle = True Then
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 1)
            End If
            If AutoClose = True Then
                TimerClose.Enabled = True

            End If


            Dim temptext As String = "Play: " & RadioName & vbNewLine & RadioURL.ToString

            If temptext.Length > 64 Then
                NotifyIcon1.Text = Microsoft.VisualBasic.Left(temptext, 63)
            Else
                NotifyIcon1.Text = "Play: " & RadioName
            End If
            If ExtractTitle = True Then
                SaveTextToFile("XtreMP3 stopped", Application.StartupPath & "\title.txt")
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Title", "XtreMP3")

            End If
        End If

        Button1.Text = Action

    End Sub

    Sub PopUpTittle()

        Form6.Label2.Text = "Current stream title"
        Try
            If CurrentTitle.Length > 60 Then
                Form6.Label1.Text = Microsoft.VisualBasic.Left(CurrentTitle, 58) & " ..."
            Else
                Form6.Label1.Text = CurrentTitle
            End If
            Form6.TopMost = True
            Form6.Show()
        Catch exc As Exception

            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
            NotifyIcon1.BalloonTipText = exc.ToString '"Error number: " & Err.Number & ". Error description: " & Err.Description & ". Trying to resume..."
            NotifyIcon1.BalloonTipTitle = "Error..."
            NotifyIcon1.ShowBalloonTip(200)
            'MsgBox(exc.Message)
        Finally

        End Try

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        On Error GoTo errh


        CurrentTitle = Microsoft.VisualBasic.Left(Player.currentMedia.getItemInfo("Title"), 63)
        If CurrentTitle <> ReturnedTitle Then
            ReturnedTitle = CurrentTitle
            NotifyIcon1.Text = CurrentTitle
            If ShowTitle = True Then
                PopUpTittle()
            End If
            If ExtractTitle = True Then
                SaveTextToFile(CurrentTitle, Application.StartupPath & "\title.txt")
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Title", CurrentTitle)
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "Status", 2)
                SaveSetting("XtreMP3", "LCDSmartieIntergration", "RawStatus", Player.status)
            End If
            Call InformServer()
        End If



        Exit Sub
errh:
        NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
        NotifyIcon1.BalloonTipText = "Error number: " & Err.Number & ". Error description: " & Err.Description & ". Trying to resume..."
        NotifyIcon1.BalloonTipTitle = "Error..."
        NotifyIcon1.ShowBalloonTip(1000)
        Resume Next

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub PlayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayToolStripMenuItem.Click
        Action = "Play"
        Call PlayStop()
    End Sub

    Private Sub StopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopToolStripMenuItem.Click
        Action = "Stop"
        Call PlayStop()
    End Sub
    Sub SaveAsFavorite()

        Dim sw As StreamWriter
        Try
            sw = File.AppendText(FavoritesPath)
            sw.Write(CurrentTitle & vbNewLine)
            sw.Flush()
        Catch exc As Exception
            MsgBox(exc.Message)

        Finally
            If Not sw Is Nothing Then
                sw.Close()
            End If
        End Try
        Form6.Label2.Text = "Added to favorites"
        Try
            If CurrentTitle.Length > 60 Then
                Form6.Label1.Text = Microsoft.VisualBasic.Left(CurrentTitle, 58) & " ..."
            Else
                Form6.Label1.Text = CurrentTitle
            End If

            Form6.Show()
        Catch exc As Exception

            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
            NotifyIcon1.BalloonTipText = exc.ToString '"Error number: " & Err.Number & ". Error description: " & Err.Description & ". Trying to resume..."
            NotifyIcon1.BalloonTipTitle = "Error..."
            NotifyIcon1.ShowBalloonTip(1000)
            'MsgBox(exc.Message)
        Finally

        End Try

    End Sub

    Private Sub NotifyIcon1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.Click
        TimerCloseInTen.Enabled = False
        TimerClose.Enabled = False

    End Sub


    Private Sub NotifyIcon1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDown
        If (Control.ModifierKeys And Keys.Shift) <> 0 Then
            Call SaveAsFavorite()

            '   MsgBox(GetFileContents(Application.StartupPath & "\favorites.txt"))
            '   MsgBox("Shift is pressed.")
        ElseIf (Control.ModifierKeys And Keys.Control) <> 0 Then
            ' MsgBox("Control is pressed.")
        ElseIf (Control.ModifierKeys And Keys.Alt) <> 0 Then
            ' MsgBox("Alt is pressed.")
        Else

            If e.Button = Windows.Forms.MouseButtons.Left Then
                Call PlayStop()
            Else

            End If
        End If


    End Sub


    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        On Error GoTo Err
        Form2.ShowDialog()
        Exit Sub
Err:
        Resume Next
    End Sub

    Sub plsx()
        RadioURL = objPLSXFile.GetString("playlist", "File1", "http://www.mediabox.gr/streams/rockfm.asx")
        RadioName = objPLSXFile.GetString("playlist", "Title1", "Radio station name not available")
        RadioName = FilterBrackets(RadioName)
        Action = "Play"
        Call PlayStop()

    End Sub

    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        Form3.Show()
    End Sub

    '###################################### text file functions #####################################
    Public Function GetFileContents(ByVal FullPath As String, _
       Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
    End Function

    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        ' Dim Contents As String
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try

            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function

    '###################################### text file functions #####################################
    Function FilterBrackets(ByVal TextToClear As String)
        On Error Resume Next
        Dim ResultText As String
        Dim TextToReplace
        TextToReplace = Microsoft.VisualBasic.Mid(TextToClear, TextToClear.IndexOf("(") + 1, (TextToClear.IndexOf(")") + 1))
        ResultText = Replace(TextToClear, TextToReplace, "")

        Return ResultText

    End Function


    Private Sub OpenPlaylistpstToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPlaylistpstToolStripMenuItem.Click
        On Error GoTo errh
        OpenFileDialog1.Title = "XtreMP3: Open Playlist File"
        OpenFileDialog1.FileName = PlaylistFileName
        OpenFileDialog1.InitialDirectory = LastPathUsed
        OpenFileDialog1.Filter = "All files (*.*)|*.*|Playlist files (*.pls)|*.pls|Playlist files (*.plsx)|*.plsx"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            objPLSXFile = New IniFile(OpenFileDialog1.FileName)
            PlaylistFileName = objPLSXFile.ToString
            LastPathUsed = OpenFileDialog1.FileName
            Call plsx()

            Exit Sub
errh:
            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
            NotifyIcon1.BalloonTipText = "Error number: " & Err.Number & ". Error description: " & Err.Description & ". Trying to resume..."
            NotifyIcon1.BalloonTipTitle = "Error..."
            NotifyIcon1.ShowBalloonTip(1000)
            Resume Next

        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.Hide()
    End Sub

    Private Sub LinkToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkToolStripMenuItem.Click
        Form5.Show()
    End Sub

    Private Sub ExitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem1.Click
        Me.Close()
    End Sub
    Public Sub Mute()
        FrmCountDown.TopMost = True
        FrmCountDown.Show()

        MuteTime = objIniFile.GetString("settings", "mutetime", 10)
        If MuteTime > 0 Then
            FrmCountDown.Timer1.Enabled = False
            FrmCountDown.Timer1.Enabled = True
        End If
    End Sub

    Private Sub TimerClose_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerClose.Tick
        TimerRounds = TimerRounds + 1
        If TimerRounds = 60 Then
            If ShowTips = True Then
                NotifyIcon1.BalloonTipText = "Player was inactive (Stopped) for a long period of time." & _
                    vbNewLine & "In order to free system resources the program will exit automatically in 10 seconds." & _
                      "If you like to keep it running on tray give a command on the tray by clicking it or press ALT+H)"
                NotifyIcon1.BalloonTipTitle = "Xtremp3 autoclose info"
                NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
                ' NotifyIcon1.
                NotifyIcon1.ShowBalloonTip(500)
            End If
            TimerRounds = 0
            TimerCloseInTen.Enabled = True
            TimerClose.Enabled = False

        End If

    End Sub

    Private Sub TimerCloseInTen_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerCloseInTen.Tick
        Me.Close()
        Form2.Close()
        Form3.Close()
        Form5.Close()
        Form6.Close()

    End Sub



    Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
        FrmHelp.ShowDialog()
    End Sub


    Private Sub FaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FaToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("1", "RadioUrl", "")
        RadioName = objIniFav.GetString("1", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If

    End Sub

    Private Sub FbToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FbToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("2", "RadioUrl", "")
        RadioName = objIniFav.GetString("2", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FcToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FcToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("3", "RadioUrl", "")
        RadioName = objIniFav.GetString("3", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FdToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FdToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("4", "RadioUrl", "")
        RadioName = objIniFav.GetString("4", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If

    End Sub

    Private Sub FeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FeToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("5", "RadioUrl", "")
        RadioName = objIniFav.GetString("5", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If

    End Sub

    Private Sub ManageFavoritesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageFavoritesToolStripMenuItem1.Click
        Try
            Process.Start(Application.StartupPath & "\stations.ini")
        Catch
            NotifyIcon1.BalloonTipIcon = ToolTipIcon.Warning
            NotifyIcon1.BalloonTipText = "Error number: " & Err.Number & ". Error description: " & Err.Description & ". Cannot open favorites file."
            NotifyIcon1.BalloonTipTitle = "Error..."
            NotifyIcon1.ShowBalloonTip(1000)

        End Try

    End Sub

    Private Sub FgToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FgToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("6", "RadioUrl", "")
        RadioName = objIniFav.GetString("6", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FhToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FhToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("7", "RadioUrl", "")
        RadioName = objIniFav.GetString("7", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FiToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("8", "RadioUrl", "")
        RadioName = objIniFav.GetString("8", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FjToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FjToolStripMenuItem.Click
        RadioURL = objIniFav.GetString("9", "RadioUrl", "")
        RadioName = objIniFav.GetString("9", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If
    End Sub

    Private Sub FkToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FkToolStripMenuItem.Click

        RadioURL = objIniFav.GetString("10", "RadioUrl", "")
        RadioName = objIniFav.GetString("10", "RadioName", "Radio station name not available")
        If RadioURL = Nothing Then
        Else
            If RadioURL.Length > 6 Then
                Action = "Play"
                Call PlayStop()
            End If
        End If


    End Sub


    Private Sub OnIncomingMessage(ByVal Args As UNOLibs.Net.ServerClass.InMessEvArgs) Handles Server.IncomingMessage
        ' AddTextToLabel(Args.message)
        SenderToReply = Args.senderIP
        '  MsgBox("sender was " & SenderToReply)
        If LCase(Args.message) = "x" Then
            ClosePlayer()
        ElseIf LCase(Args.message) = "s" Then
            Action = "Stop"
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf PlayStop))
            Else
                PlayStop()
            End If

        ElseIf LCase(Args.message) = "p" Then
            Action = "Play"
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf PlayStop))
            Else
                PlayStop()
            End If
        ElseIf LCase(Args.message) = "t" Then
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf PopUpTittle))
            Else
                PopUpTittle()
            End If
            Try
                Client.SendMessage(SenderToReply, 40000, CurrentTitle)
            Catch ex As Exception
                'Label1.Text = ex.Message ' Err.Description
            End Try
        ElseIf LCase(Args.message) = "m" Then
            If WaitForRemoteCommands = True Then
                If Me.InvokeRequired Then
                    Me.Invoke(New MethodInvoker(AddressOf Mute))
                Else
                    PopUpTittle()
                End If
                Try
                    Client.SendMessage(SenderToReply, 40000, "Muted")
                Catch ex As Exception
                    'Label1.Text = ex.Message ' Err.Description
                End Try
            End If
        Else
        End If
       

    End Sub


    Public Sub SendUnMute()
        If WaitForRemoteCommands = True Then

            Try
                Client.SendMessage(SenderToReply, 40000, "unMuted")
            Catch ex As Exception

            End Try
        End If
    End Sub


    Private Sub ClosePlayer()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf Me.Close))
        Else
            Me.Close()
        End If
    End Sub

    ' '' Solution below for the cross threading error (wher the label cannot be updated while the data received from a fdifferent thead
    ''Private Delegate Sub AddTextToLabelDelegate(ByVal someText As String)

    ''Private Sub AddTextToLabel(ByVal someText As String)
    ''    'When this sub is called, test which thread we are in.
    ''    'InvokeRequired does this, it returns true if the control
    ''    'cannot be accessed in the current thread
    ''    If Me.InvokeRequired Then
    ''        'We are in the wrong thread.
    ''        'Use a delegate to call this sub again from the correct thread:
    ''        Dim delegate1 As New AddTextToLabelDelegate(AddressOf AddTextToLabel)
    ''        'Need to send the sub parameters, use an object:
    ''        Dim parameters(0) As Object
    ''        parameters(0) = someText
    ''        'Call the sub again:
    ''        Me.Invoke(delegate1, parameters)
    ''    Else
    ''        'This time the sub was called from the correct thread
    ''        'So we can alter label1
    ''        Label2.Text = someText
    ''    End If
    ''End Sub

    ' '' Solution below for the cross threading error (wher the label cannot be updated while the data received from a fdifferent thead

End Class
